from django.shortcuts import render

# Create your views here.

from .models import *
from .ser import *
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.paginator import Paginator

class JournalismM(APIView):
    def post(self,request):
        datas = JournalismSer(data=request.data)
        if datas.is_valid():
            datas.save()
            return Response({"code":200,"list":datas.data})
        else:
            print(datas.errors)
            return Response({"code": 10010})
    def get(self,request):
        datas = Journalism.objects.all()
        cpage = request.GET.get("cpage")

        p = Paginator(datas,2)
        stu = p.page(cpage)
        ser = JournalismSer(stu,many=True)
        return Response({"code":200,'list':ser.data,'tpage':p.num_pages})

class AdvertisingM(APIView):
    def post(self,request):
        datas = AdvertisingSer(data=request.data)
        print(request.data)
        # return Response({"code":200})
        if datas.is_valid():
            datas.save()
            return Response({"code":200,'list':datas.data})
        else:
            print(datas.errors)
            return Response({"code":10010})